<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.cn/ ShopEx License
 */

/**
 * ctl_cart
 *
 * @uses b2c_frontpage
 * @package
 * @version $Id: ctl.cart.php 1952 2008-04-25 10:16:07Z flaboy $
 * @copyright 2003-2007 ShopEx
 * @author <kxgsy163@163.com>
 * @license Commercial
 */

class whyshopseckill_ctl_wap_whyshopseckill extends wap_frontpage{


   /* function __construct($app){
        parent::__construct($app);
        $shopname = app::get('site')->getConf('site.name');
        $this->shopname = $shopname;
        if(isset($shopname)){
            $this->title = app::get('b2c')->_('添加秒杀商品').$shopname;
            $this->keywords = app::get('b2c')->_('添加秒杀商品').$shopname;
            $this->description = app::get('b2c')->_('添加秒杀商品').$shopname;
        }
        $this->app_b2c = app::get('b2c');
      }*/
var $noCache = true;//不用缓存2017-05-03 15:52:06
    function  testwxtemplate(){
        $libwxtmpmsg=kernel::single('weixin_templatemsg');
        $libwxtmpmsg->loadWxTemplateMsg();
    }
function  testcronb(){
    kernel::single("whyshopseckill_task_yuyue")->exec();
}

      public function getgoods($id) {
       
        $id=intval($_POST['id'])?intval($_POST['id']):$id;

        $filter['whyshopseckill_id']=$id;
        //取当前秒杀活动下的所有秒杀商品信息


          $seckill_basic=$this->app->model('whyshopseckill')->dump($id);

         // print_r($seckill_basic);_exit;



        $arr_goods = $this->app->model('whyshopseckill_goods')->getList('*',$filter,0,-1); 
      
        $userObject = kernel::single('b2c_user_object');
        $siteMember = $userObject->get_current_member();
$seckill_status='';
          $now_time=time();
              if($seckill_basic['seckill_starttime']>$now_time){
                  $seckill_status='notbegin';
              }
              elseif($seckill_basic['seckill_starttime']<=$now_time && $seckill_basic['seckill_endtime']>$now_time){
                  $seckill_status='';//已经开始了为空

              }

          $mdlYuyue = app::get('whyshopseckill')->model('yuyue');

        foreach ($arr_goods as $key => $value) {
            /*判断是否已添加过提醒*/

            $exist_yuyue=$mdlYuyue->getRow('whyshopseckill_id',array('member_id'=>$siteMember['member_id'],'whyshopseckill_id'=>$id,'whyshopseckill_goods_id'=>$value['seckill_goods_id']));
            if($exist_yuyue){
                $arr_goods[$key]['hasnotify']=1;
            }
            /*判断是否已添加过提醒end*/



            # code...
          $filter['goods_id']=$value['seckill_goods_id'];


          $arr_goods_info = app::get('b2c')->model('goods')->getList('*',$filter,0,-1);

          // print_r($arr_goods_info);exit;

          $filter['is_default']='true';
          $arr_product_info = app::get('b2c')->model('products')->getList('*',$filter,0,-1);




          $name=$arr_product_info[0]['name'];
          $arr_goods[$key]['goods_name']=$name;


          $products=$arr_product_info[0]['product_id'];
          $arr_goods[$key]['product_id']=$products;


          $mktprice=$arr_goods_info[0]['mktprice'];
          $arr_goods[$key]['goods_mktprice']=$mktprice;

          //  $arr_goods_image = app::get('b2c')->model('goods')->getList('*',$filter,0,-1);
          $image=$arr_goods_info[0]['image_default_id'];
          $arr_goods[$key]['goods_image']=$image;

          $arr_static = $this->app->model('whyshopseckill')->getList('*',$filter,0,-1);  

          $static=$arr_static[0]['seckill_static'];

          $arr_goods[$key]['static']=$static;

          //获取秒杀商品出售情况
          $goods_id=$arr_goods_info[0]['goods_id'];
          $whyshopseckill_id=$id;
          $member_id=$siteMember['member_id'];
          $arr_goods[$key]['member_id']=$member_id;
          $seckill_sum=$this->app->model('whyshopseckill')->getSeckillgoodsnums($goods_id,$whyshopseckill_id);

          if ($seckill_sum['total']=='') {
           $seckill_sum['total']=0;
          }
          $sum=$seckill_sum['total'];

          //  file_put_contents(DATA_DIR.'/testseckill.txt',date("Y-m-d H:i:s").var_export($sum,true)."\r\n",FILE_APPEND);
          $arr_goods[$key]['sum']=$sum;


          $percent=ceil(($arr_goods[$key]['sum']+$arr_goods[$key]['seckill_virtual_count'])/($arr_goods[$key]['seckill_total']+$arr_goods[$key]['seckill_virtual_count'])*100);
           $arr_goods[$key]['percent']=$percent;


            if($percent==100){
               // $seckill_status='soldout';
                 /**/
            $arr_goods[$key]['seckill_status']='soldout';
            /**/
            }
            else{
              $arr_goods[$key]['seckill_status']=$seckill_status;
            }
    

           //限购2件
            $member_id=$siteMember['member_id'];
            $arr_goods[$key]['member_id']=$member_id;
            $member_purchase=$this->app->model('whyshopseckill')->getSeckillgoodsnumslimit($goods_id,$whyshopseckill_id,$member_id);
            //每个会员购买数量
            $purchase=$member_purchase['total'];
            $arr_goods[$key]['purchase']=$purchase;

            /**/
            //$arr_goods[$key]['seckill_status']=$seckill_status;
            /**/
            $arr_goods[$key]['seckill_starttime_hour']=date("H:00",$seckill_basic['seckill_starttime']);
        }


         // print_r($arr_goods);exit();
       //echo json_encode($arr_goods);
        // if ($member_id && $purchase<=$seckill_limit ) {
        //   # code...
        // }
        $this->pagedata['arr_goods5']=$arr_goods;




        $this->display('wap/whyshopseckill/goods.html');

      }
      public function index() {

       $seckill_time=$this->app->model('whyshopseckill')->getSeckillTime();
          //print_r($seckill_time);exit;
       $this->pagedata['whyshopseckill_time'] = $seckill_time;
       $this->pagedata['res_url'] =app::get('whyshopseckill')->res_url;
       $this->page('wap/whyshopseckill/index.html');
     }


     public function index3($id){
       $arr = app::get('b2c')->model('goods')->dump($id);
       $whyshopseckill_info=$this->app->model('whyshopseckill')->getSeckillByGoodsId();        
       $this->pagedata['whyshopseckill_info'] = $whyshopseckill_info;
       $this->pagedata['res_url'] =app::get('whyshopseckill')->res_url;
        $this->page('wap/whyshopseckill/index.html');
     }
    //添加预约
    function  addyuyue(){
        $mdlSecGoods = app::get('whyshopseckill')->model('whyshopseckill_goods');
        $mdlSec= app::get('whyshopseckill')->model('whyshopseckill');

        $whyshopseckill_id=intval($_POST['whyshopseckill_id']);
        $seckill_goods_id=intval($_POST['seckill_goods_id']);

        $seckill_product_id=intval($_POST['seckill_product_id']);


        $rs_seckill_goods=$mdlSecGoods->getList('whyshopseckill_goods_id',array('whyshopseckill_id'=>$whyshopseckill_id,'seckill_goods_id'=>$seckill_goods_id));

        $rs_seckill=$mdlSec->getRow('whyshopseckill_id,seckill_starttime',array('whyshopseckill_id'=>$whyshopseckill_id));

        if($rs_seckill && $rs_seckill_goods) {
            $userObject = kernel::single('b2c_user_object');
            $siteMember = $userObject->get_current_member();
            if ($siteMember['member_id']) {

                $mdlPam = app::get('pam')->model('bind_tag');
                $weixinbind = $mdlPam->getRow('open_id',array('member_id'=>$siteMember['member_id']));
                if (!$weixinbind['open_id']) {
                    $this->splash('failed', null, app::get('b2c')->_('请先关注公众号才能使用提醒功能'),'','',true);
                } else {
                    $mdlYuyue = app::get('whyshopseckill')->model('yuyue');
                    $exist_yuyue=$mdlYuyue->getRow('whyshopseckill_id',array('member_id'=>$siteMember['member_id'],'whyshopseckill_id'=>$whyshopseckill_id,'whyshopseckill_goods_id'=>$seckill_goods_id));

                    if(!$exist_yuyue) {
                        $aData['member_id'] = $siteMember['member_id'];
                        $aData['open_id'] = $weixinbind['open_id'];
                        $aData['seckill_starttime'] = $rs_seckill['seckill_starttime'];
                        $aData['whyshopseckill_id'] = $whyshopseckill_id;
                        $aData['whyshopseckill_goods_id'] = $seckill_goods_id;
                        $aData['whyshopseckill_product_id'] = $seckill_product_id;


                        $ret = $mdlYuyue->save($aData);
                        if ($ret) {
                            $this->splash('success', null, app::get('b2c')->_('设置提醒成功，请关注公众号，留意微信通知'), '', '', true);
                        } else {
                            $this->splash('failed', null, app::get('b2c')->_('添加提醒失败'), '', '', true);
                        }
                    }
                    else{
                        $this->splash('failed', null, app::get('b2c')->_('您已添加过提醒'), '', '', true);

                    }
                }

            } else {
                $url = $this->gen_url(array('app'=>'b2c','ctl'=>'wap_passport','act'=>'login'));
                $this->splash('failed', $url, app::get('b2c')->_('请先登录'), '','',true);

            }
        }
        else{
            $this->splash('failed', null, app::get('b2c')->_('秒杀商品信息获取失败'), '','',true);
        }
    }

    
   }

